/*
  包含多个 接收组件通知触发mutation调用间接更新状态的方法对象
 */

import {ADD_TODO, DELETE_TODO, SELECT_ALL_TODOS, CLEAR_ALL_COMPLETE} from './mutation-types'
import storageUtils from "../util/storageUtils";

export default {

  // 添加todos
  addTodo({commit}, todo){
    // 提交对mutation的请求
    commit(ADD_TODO, {todo})
  },

  // 删除todos
  deleteTodo({commit}, index){
    commit(DELETE_TODO, {index})
  },

  // 全选/全不选todos
  selectALLTodos({commit}, check){
    commit(SELECT_ALL_TODOS, {check})
  },

  // 删除全部选中的
  clearAllComplete({commit}){
    commit(CLEAR_ALL_COMPLETE)
  },

  // 异步获取todos并更新状态
  reqTodos({commit}){
    // 模拟
    setTimeout(() =>{
      // 读取数据
      const todos = storageUtils.readTodos()
      // 更新状态
      commit(RECEIVE_TODOS, todos)
    }, 1000)
  }
}
